package observer03;

import observer03.imp.Observer;

import java.util.concurrent.CopyOnWriteArrayList;

public abstract class Subject {

    //定义一个观察者数组，支持并发
    private CopyOnWriteArrayList<Observer> observers = new CopyOnWriteArrayList<>();

    public void addObserver(Observer o){
        this.observers.add(o);
    }

    public void delObserver(Observer o){
        this.observers.remove(o);
    }

    public void notifyObservers(String context){
        for(Observer observer : observers){
            observer.update(context);
        }
    }

}
